unit cx_nbrb_by;

interface
uses
  SysUtils,
  cx_vars,
  DateUtils,
  m_api,
  m_globaldefs,
  cx_http,
  cx_XML;

function NBRBby_GetRate(CurrencyTo: String; CurrencyFrom: String = ''): TRates;

implementation
uses
  Windows,
  cx_procs;

const
  cx_nbrbby_url = 'http://www.nbrb.by/Services/XmlExRates.aspx';

  //xml tags
  cx_DailyExRates = 'DailyExRates';
  cx_Currency = 'Currency';
  cx_Date = 'Date';
  cx_Scale = 'Scale';
  cx_Rate = 'Rate';
  cx_Id = 'Id';

function TransformCurrency(Currency:string) : string;
begin
if currency='ATS' then result:= '7';
if currency='AUD' then result:= '170';
if currency='BEF' then result:= '12';
if currency='BGL' then result:= '6';
if currency='BGN' then result:= '191';
if currency='CAD' then result:= '23';
if currency='CHF' then result:= '130';
if currency='CNY' then result:= '254';
if currency='CZK' then result:= '171';
if currency='DEM' then result:= '200';
if currency='DKK' then result:= '36';
//if currency='EEK' then result:= '172';
if currency='EEK' then result:= '225';
if currency='ESP' then result:= '125';
if currency='EUR' then result:= '19';
if currency='FIM' then result:= '43';
if currency='FRF' then result:= '44';
if currency='GBP' then result:= '143';
if currency='IEP' then result:= '63';
if currency='ISK' then result:= '58';
if currency='ITL' then result:= '65';
if currency='JPY' then result:= '67';
//if currency='KGS' then result:= '175';
if currency='KGS' then result:= '223';
if currency='KWD' then result:= '72';
//if currency='KZT' then result:= '174';
if currency='KZT' then result:= '222';
if currency='LTL' then result:= '177';
if currency='LUF' then result:= '78';
if currency='LVL' then result:= '176';
if currency='MDL' then result:= '178';
if currency='NLG' then result:= '94';
if currency='NOK' then result:= '101';
//if currency='PLN' then result:= '188';
if currency='PLN' then result:= '219';
if currency='PLZ' then result:= '108';
if currency='PTE' then result:= '109';
//if currency='RUB' then result:= '190';
if currency='RUR' then result:= '190';
if currency='SEK' then result:= '129';
if currency='SGD' then result:= '119';
if currency='TRL' then result:= '137';
if currency='TRY' then result:= '220';
//if currency='UAH' then result:= '169';
if currency='UAH' then result:= '224';
if currency='USD' then result:= '145';
if currency='XDR' then result:= '166';
end;

function NBRBby_GetRate(CurrencyTo: String; CurrencyFrom: String = ''): TRates;
 var
  i, qty, nc: Integer;
  value, todayrate : Real;
  RateDate : TDateTime;
  //tmr, tdy, rdt : string;
  response: WideString;
  //
  root, actnode, datanode: HXML;
  tmp: PWideChar;
  //
  CurrId, CurrId2, NodeId: String;
begin
  result.success:=false;
  //
  result.coming.rate:=0;
  result.coming.date:=cx_Tomorrow;
  result.current.rate:=0;
  result.current.date:=cx_Today;
  //result.previous.rate:=0;
  //result.previous.date:=cx_DateFrom;
  //
  CurrId:=TransformCurrency(CurrencyTo);
  if CurrencyFrom<>'' then
    CurrId2:=TransformCurrency(CurrencyFrom);
  //
  response:=HTTP_NL_Get(cx_nbrbby_url);
  //
  if response='' then //no response - may be network failure
    Exit;
  with cx_XMLw do
   begin
    try
     root:=parseString(PWideChar(response), @i, cx_DailyExRates);
     //root:=parseString(tmp, @i, cx_ValCurs);
    except
     MessageBox(0, 'Parse error', piShortName, MB_OK or MB_ICONERROR);
     root:=0;
    end;
    if root<>0 then
     begin
      DecimalSeparator:='.';
      RateDate:=Date;
      nc:=getChildCount(root);
      for i:=0 to nc-1 do
       begin
        actnode:=getNthChild(root, cx_Currency, i);
        if actnode=0 then
          Continue;
        tmp:=getAttrValue(actnode, cx_Id);
        if tmp<> nil then
         begin
          //ShowMessage(tmp); //debug
          NodeId:=WideCharToString(tmp);
          //MessageBox(0, PChar(NodeID), piShortName, MB_OK or MB_ICONINFORMATION);
         end
        else
          NodeId:='';
        //
        if NodeID=CurrId then
         begin
          datanode:=getNthChild(actnode, cx_Scale, 0);
          if datanode=0 then
            qty:=0
          else
           begin
            tmp:=getText(datanode);
            if tmp=nil then
              qty:=0
            else
             begin
              //ShowMessage(tmp); //debug
              qty := StrToInt(tmp);
             end;
           end;
          //MessageBox(0, PChar(IntToStr(qty)), piShortName, MB_OK or MB_ICONINFORMATION);
          //
          datanode:=getNthChild(actnode, cx_Rate, 0);
          if datanode=0 then
            value:=0
          else
           begin
            tmp:=getText(datanode);
            if tmp=nil then
              value:=0
            else
             begin
              //ShowMessage(tmp); //debug
              value:=StrToFloat(tmp);
             end;
           end;
          //MessageBox(0, PChar(FloatToStr(value)), piShortName, MB_OK or MB_ICONINFORMATION);
          if qty<>0 then
            todayrate:=value/qty
          else
            todayrate:=0;
          //MessageBox(0, PChar(FloatToStr(todayrate)), piShortName, MB_OK or MB_ICONINFORMATION);

          result.current.rate:=todayrate;
          result.current.date:=RateDate;

          Break;
         end;
       end;
      destroyNode(root);
      //
      if result.current.rate=0  then
       begin
        if result.previous.rate<>0 then
          result.current.rate:=result.previous.rate;
       end;
      result.success:=true;
     end
    else
     begin
      //not parced
      //MessageBox(0, 'Zero root!', piShortName, MB_OK or MB_ICONEXCLAMATION);
     end;
   end;
end;

end.
